<?php

namespace app\admin\service;

use app\common\Error;
use app\model\User;
use app\model\Admin;
use app\model\Applet;

class AppletService
{
    private $model;


    private $adminModel;
    private $adminRuleModel;


    public function __construct(User $model , Admin $adminModel, Applet $appletModel)
    {
        $this->model = $model;


        $this->adminModel = $adminModel;
   

        $this->appletModel = $appletModel;
    }

    // 获取用户列表
    public function getUserList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where[] = ['is_delete', '=', 0];

        // 用户id搜索
        if (!empty($params['id'])) {
            $where[] = ['id', '=', $params['id']];
        }

        if (!empty($params['official_account_id'])) {
            $where[] = ['official_account_id', '=', $params['official_account_id']];
        }

        if (!empty($params['applet_id'])) {
            $where[] = ['applet_id', '=', $params['applet_id']];
        }

        if (!empty($params['start_date'])) {
            $startDate = strtotime($params['start_date']);
            $where[] = ['created_at', '>', $startDate];
        }

        if (!empty($params['end_date'])) {
            $endDate = strtotime($params['end_date'] . ' 23:59:59');
            $where[] = ['created_at', '<', $endDate];
        }

        $data = $this->model->getPagingDataByWhere($where, $page, $limit, 'id, user_name, official_account_id, applet_id, avatar, official_account_created_at, created_at, activation_time, link_id, status, closing_time, official_account_open_id, mobile_system');

        if (!empty($data['data'])) {

        }

        return $data;
    }

    // 获取用户详情
    public function getUserDetail($id)
    {
        $fields = 'id, user_name, official_account_open_id, open_id, money, official_account_id';
        $data = $this->model->getInfoById($id, $fields);


        return $data;
    }

    /**
     * 获取管理员列表
     */
    public function getManagerList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = ['status' => 1, 'is_delete' => 1];
        $data = $this->adminModel->getPagingDataByWhere($where, $page, $limit, 'id, rule_id, name, nickname, profile_photo, fans_count, introduce');

        $ruleIds = array_column($data['data'], 'rule_id');
        $ruleData = $this->adminRuleModel->getDataByWhere(['id' => $ruleIds], 'id, name');
        $ruleResult = array_column($ruleData, 'name', 'id');

        foreach ($data['data'] as $key => $item) {
            $data['data'][$key]['rule_name'] = empty($ruleResult[$item['rule_id']]) ? '' : $ruleResult[$item['rule_id']];
        }

        return $data;
    }

    // 获取管理员详情
    public function getManagerDetail($id)
    {
        $data = $this->adminModel->getInfoById($id, '*');
        return $data;
    }

    // 添加/修改管理员
    public function editManager($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);

            // 判断用户名有没有重名的
            $data = $this->adminModel->getInfoByWhere(['name' => $params['name']], 'id');
            if ($data) {
                return ['message' => Error::USERNAME_EXISTS];
            }
        } else {
            $params['updated_at'] = time();
            $data = $this->adminModel->getInfoById($params['id'], 'password');

            if ($data['password'] != $params['password']) { // 修改过
                $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
            }
        }

        $id = $this->adminModel->edit($params);

        if (!$id) {
            return false;
        }

        return true;
    }

    // 删除管理员
    public function deleteManager($id)
    {
        if (in_array($id, config('enum.admin_id'))) {
            return ['message' => Error::DELETE_MANAGER_ERROR_MESSAGE];
        }

        $count = $this->adminModel->getCounts();

        if ($count <= 1) {
            return false;
        }

        $bool = $this->adminModel->deleteAdminManager($id);

        if (!$bool) {
            return false;
        }

        return true;
    }

    // 管理员下拉列表
    public function getManagerSelectList()
    {
        $where = ['status' => 1, 'is_delete' => 1];
        $data = $this->adminModel->getDataByWhere($where, 'id, nickname as name, profile_photo');
        return $data;
    }

    // 管理员下拉列表
    public function getruleSelectList()
    {
        $where = ['status' => 1, 'is_delete' => 1, 'rule' => 1];
        $data = $this->adminModel->getDataByWhere($where, 'id, nickname as name, profile_photo');
        return $data;
    }

    // 获取用户投诉建议列表
    public function getUserProposalList($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $where = [];
        // 用户id搜索
        if (!empty($params['uid'])) {
            $where[] = ['uid', '=', $params['id']];
        }

        if (!empty($params['app_id'])) {
            $where[] = ['app_id', '=', $params['app_id']];
        }

        if (!empty($params['start_date'])) {
            $startDate = strtotime($params['start_date']);
            $where[] = ['created_at', '>', $startDate];
        }

        if (!empty($params['end_date'])) {
            $endDate = strtotime($params['end_date'] . ' 23:59:59');
            $where[] = ['created_at', '<', $endDate];
        }

        $data = $this->proposalModel->getPagingDataByWhere($where, $page, $limit, 'id, app_id, uid, phone, title, images, content, source, is_handle, created_at');
        if ($data['data']) {
            $appIds = array_column($data['data'], 'app_id');
            foreach ($data['data'] as $key => $item) {
//
                if ($item['images']) {
                    $images = json_decode($item['images'], true);
                    $data['data'][$key]['images'] = $images[0];
                }

                if ($item['title']) {
                    $data['data'][$key]['title'] = json_decode($item['title'], true);
                }
            }
        }
        return $data;
    }

    // 投诉建议详情
    public function getUserProposalDetail($id)
    {
        $data = $this->proposalModel->getInfoByWhere(['id' => $id], 'id, app_id, uid, title, images, content, is_handle');
        if ($data['images']) {
            $data['data'] = json_decode($data['images'], true);
        }
        return $data;
    }
}